package com.atm.atmsys.Service.Imp;

import com.atm.atmsys.Mapper.UserMapper.UserMapper;
import com.atm.atmsys.Service.UserService;
import com.atm.atmsys.constant.PasswordConstant;
import com.atm.atmsys.context.BaseContext;
import com.atm.atmsys.dto.UserDTO;
import com.atm.atmsys.dto.UserPageDTO;
import com.atm.atmsys.entity.AdminInfo;
import com.atm.atmsys.entity.PageResult;
import com.atm.atmsys.entity.UserInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserMapper userMapper;

    //用户登录
    @Override
    public UserInfo login(UserInfo userInfo) {
        String password = DigestUtils.md5DigestAsHex(userInfo.getUserPassword().getBytes());
        userInfo.setUserPassword(password);
        return userMapper.getByUserNameAndPassword(userInfo);
    }

    //添加用户
    @Override
    public void addUser(UserInfo userInfo) {
        //设置用户的默认密码为123456
        userInfo.setUserPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DefaultPassword.getBytes()));

        //设置用户的创建时间和更新时间
        userInfo.setCreatTime(LocalDateTime.now());
        userInfo.setUpdateTime(LocalDateTime.now());

        userMapper.addUser(userInfo);
    }

    //删除用户
    @Override
    public void deleteAdmin(String identification) {
        userMapper.deleteUser(identification);
    }

    //修改用户信息
    @Override
    public void updateUser(UserDTO userDTO) {
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userDTO, userInfo);
        System.out.println(userInfo);
        userInfo.setUpdateTime(LocalDateTime.now());
        userInfo.setUserPassword(DigestUtils.md5DigestAsHex(userInfo.getUserPassword().getBytes()));
        userMapper.updateUser(userInfo);
    }

    //分页查询用户信息
    @Override
    public PageResult pageQuery(UserPageDTO userPageDTO) {
        PageHelper.startPage(userPageDTO.getPage(), userPageDTO.getPageSize());
        Page<UserInfo> page= userMapper.pageQuery(userPageDTO);
        long total = page.getTotal();
        List<UserInfo> result = page.getResult();
        return new PageResult(total,result);
    }

    //根据id查询用户信息
    @Override
    public UserInfo getById(int id) {
        UserInfo userInfo = userMapper.getById(id);
        return userInfo;
    }

    @Override
    public UserInfo getBySF(String identification) {
        UserInfo userInfo= userMapper.getBySF(identification);
        return userInfo;
    }

    //根据身份证号查询用户信息
    @Override
    public UserInfo getByIdentification(String identification) {
        UserInfo userInfo = userMapper.getByIdentification(identification);
        return userInfo;
    }

    @Override
    public UserInfo getByName(String userName) {
        UserInfo userInfo = new UserInfo();
        userInfo=userMapper.getByName(userName);
        return userInfo;
    }


}
